<template>
  <div class="login">
    <div class="login_top">
      <svg class="login_top_icon" aria-hidden="true" @click="router.push('/')">
        <use xlink:href="#icon-wangyiyunyinle-6"></use>
      </svg>
    </div>
    <van-form @submit="onSubmit" class="login_form">
      <van-cell-group inset class="form_group">
        <van-field
          v-model="input.name"
          name="用户名"
          label="用户名"
          placeholder="用户名"
          :rules="useNameRules"
          class="form_field"
        />
        <van-field
          v-model="input.password"
          type="password"
          name="密码"
          label="密码"
          placeholder="密码"
          :rules="passwordRules"
          class="form_field"
        />
      </van-cell-group>
      <div style="margin: 16px">
        <van-button
          round
          block
          type="default"
          native-type="submit"
          class="form_btn"
          @click="loginEvent"
        >
          提交
        </van-button>
      </div>
    </van-form>
    <div class="login_footer">
      <div class="icon_footer">
        <svg class="icon" aria-hidden="true">
          <use xlink:href="#icon-weixin"></use>
        </svg>
        <svg class="icon" aria-hidden="true">
          <use xlink:href="#icon-weibo1"></use>
        </svg>
        <svg class="icon" aria-hidden="true">
          <use xlink:href="#icon-wangyi1"></use>
        </svg>
      </div>
      <div class="text_footer">
        <van-checkbox v-model="checked" checked-color="#ee0a24" class="text_ckb">同意</van-checkbox>
        <span class="text_span">服务条款,隐私政策</span>
      </div>
    </div>
  </div>
</template>

<script lang="ts" setup>
import { computed, reactive, ref } from 'vue'
import { useRouter } from 'vue-router'
import cache from '@/utils/cache/cache'
import { useStore } from '@/store'

const store = useStore()
const router = useRouter()
//复选框
const checked = ref(true)
//用户名和密码框
const input = reactive({
  name: cache.getCache('name') ?? '',
  password: cache.getCache('password') ?? ''
})
//用户名和密码框的校验规则
const useNameRules: any = [
  { required: true, message: '请输入账号', trigger: 'onBlur' },
  {
    pattern: /^[a-zA-Z0-9_-]{4,16}$/,
    message: '请输入4到16位(字母,数字,下划线,减号)',
    trigger: 'onBlur'
  }
]
const passwordRules: any = [
  { required: true, message: '请输入账号', trigger: 'onBlur' },
  {
    pattern: /^[a-zA-Z0-9_-]{4,16}$/,
    message: '请输入4到16位(字母,数字,下划线,减号)',
    trigger: 'onBlur'
  }
]
//储存账号密码
const onSubmit = (values: any) => {
  cache.setCache('name', input.name)
  cache.setCache('password', input.password)
}

//登录事件
const loginEvent = async () => {
  await store.dispatch('login/getUseInfo', input)
  console.log(input)

  if (cache.getCache('token')) {
    router.push('/user')
  }
}
</script>

<style scoped lang="less">
.van-button--normal {
  padding: var(--van-button-normal-padding);
  font-size: 0.35rem !important;
}

.login {
  width: 100%;
  height: 100%;
  background-color: rgb(222, 6, 6);
  display: flex;
  flex-direction: column;
  justify-content: space-around;
  align-content: center;
  position: relative;
  .login_top {
    flex: 5;
    display: flex;
    justify-content: center;
    align-items: center;
    .login_top_icon {
      width: 5rem;
      height: 5rem;
    }
  }
  .login_form {
    flex: 3;
    height: 4rem;
    display: flex;
    flex-direction: column;
    .form_group {
      flex: 2;
      display: flex;
      flex-direction: column;
      justify-content: space-around;
      align-content: center;
    }
    .form_btn {
      flex: 1;
      color: rgb(222, 6, 6);
    }
  }
  .login_footer {
    flex: 1;
    width: 100%;
    height: 1.5rem;
    display: flex;
    flex-direction: column;
    justify-content: space-around;
    .icon_footer {
      display: flex;
      justify-content: space-around;
      .icon {
        width: 0.5rem;
        height: 0.5rem;
      }
    }
    .text_footer {
      display: flex;
      justify-content: center;
      align-items: center;
      .text_ckb {
        color: antiquewhite;
        margin-right: 0.1rem;
        position: relative;
        bottom: 0.03rem;
      }
      .text_span {
        line-height: 0.5rem;
        margin-left: 0.1rem;
      }
    }
  }
}
</style>
<style>
.van-checkbox__label {
  margin-left: var(--van-checkbox-label-margin);
  color: rgb(151, 150, 150) !important;
  line-height: var(--van-checkbox-size);
}
</style>
